package uk.ac.qub.midorfeed;

import uk.ac.qub.midorfeed.R;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * Start screen activity for the app
 * 
 * @author Graeme
 * 
 */
public class StartScreenActivity extends Activity {

	/**
	 * MediaPlayer used to play background sound
	 */
	private MediaPlayer mp;

	/**
	 * public static boolean to tell app when to not play sound
	 */
	public static boolean playSound = true;

	/**
	 * On create method
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start_screen);
		initialiseMediaPlayer();
		setButtonClickListener();

	}

	/**
	 * Method to set button click listeners
	 */
	private void setButtonClickListener() {
		// start button listener
		Button startButton = (Button) findViewById(R.id.firstScreenStartBtn);
		startButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent teamPickerIntent = new Intent(StartScreenActivity.this,
						TeamPickerActivity.class);
				startActivity(teamPickerIntent);

			}
		});
		// exit button listener
		Button exitButton = (Button) findViewById(R.id.exit_app_btn);
		exitButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				System.exit(0);
			}
		});

		Button howToUseButton = (Button) findViewById(R.id.howToUseBtn);
		howToUseButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(
						StartScreenActivity.this);
				alertDialog.setTitle("How to use");
				alertDialog
						.setMessage("Click start and input friendly and enemy heroes that have already been suggested.\n\n Click pick team to return a list of suggested heroes to play");
				alertDialog.setNegativeButton("Return",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						});
				AlertDialog dialog = alertDialog.create();
				dialog.show();
			}
		});

	}

	/**
	 * Method to inflate options menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.start_screen, menu);
		return true;
	}

	/**
	 * Method to dectect when an options item is selected
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			showSettingsDialog();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * On start method
	 */
	@Override
	public void onStart() {

		super.onStart();

		if (playSound) {
			mp.start();
		}
	}

	/**
	 * On pause method
	 */
	@Override
	public void onPause() {

		super.onPause();
		if (mp != null) {
			mp.stop();
			mp = null;
		}
	}

	/**
	 * On stop method
	 */
	@Override
	public void onStop() {

		super.onStop();

		if (mp != null) {
			mp.stop();
			mp = null;
		}
	}

	/**
	 * On resume method
	 */
	@Override
	public void onResume() {
		super.onResume();
	}

	/**
	 * On restart method
	 */
	@Override
	public void onRestart() {
		super.onRestart();
		initialiseMediaPlayer();
		if (playSound) {
			mp.start();
		}
	}

	/**
	 * Checks if the mediaplayer has been instantiated, if not it instantiates
	 * it and initialises it
	 */
	private void initialiseMediaPlayer() {
		if (mp == null) {
			mp = MediaPlayer.create(this, R.raw.theme_music);
			mp.setLooping(true);
		}
	}

	/**
	 * Method to show settings dialog box
	 */
	protected void showSettingsDialog() {

		// creates dialog box
		final Dialog dialog = new Dialog(StartScreenActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.settings_fragment);

		final CheckBox checkBox = (CheckBox) dialog
				.findViewById(R.id.sound_check_box); // sound check box to
														// disable activity
														// sound

		if (playSound) {
			checkBox.setChecked(true);
		} else {
			checkBox.setChecked(false);
		}

		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			// on checked listener for app sound
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (checkBox.isChecked()) {
					playSound = true;
					if (!mp.isPlaying()) {
						mp.start();
					}
				} else {
					playSound = false;
					mp.stop();
				}

			}

		});
		// button to close dialog box
		Button finishButton = (Button) dialog
				.findViewById(R.id.close_settings_btn);
		finishButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();

			}
		});

		dialog.show();

	}
}
